// pages/tuocao/tuocao.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:'',
    tuocao:[],
    tuocaos:[],
    falg:0,
    imgurl:'',
    isLoading:false,
    pageSize:5,
    page:1,
    totalPage:0
  },
  getad:function(){
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/ads/tucaoadv', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, 'ad 成功')
        this.setData({
          imgurl: res.data.ad
        })
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  //
  gotoAddcon:function(){
    wx.navigateTo({
      url: '/pages/tocaoadd/tucaoadd'
    })
  },
  //
  gotocommons:function(e){
    // console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/tucaocomments/tucaocomments?id=' + e.currentTarget.dataset.id,
    })
  },
  //图片加载完成
  imgloaded: function(e) {
    console.log('图片加载');
    var isshowurl = "tuocao[" + e.currentTarget.dataset.index + "].images[" + e.currentTarget.dataset.imgindex +"].ishow"
    this.setData({
      [isshowurl]: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl,
    })
    this.getad()
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/trashes', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '成帖zh')
        if (res.statusCode == 401) {
          wx.clearStorage({
            success: () => {
              wx.navigateTo({
                url: '/pages/login/login',
              })
            }
          })
        }
        res.data.forEach((item)=>{
          var imgs = []
          for (var i = 0; i < item.images.length; i++) {
            imgs.push({
              imgurl: item.images[i],
              ishow: false
            })
          }
          item.images = imgs;
        })
        var tucaoslice = this.sliceArray(res.data,0,5);
        // console.log(tucaoslice,"jjjj");
        var totalPage = res.data.length % this.data.pageSize == 0 ? res.data.length / this.data.pageSize : (res.data.length - res.data.length % this.data.pageSize) / this.data.pageSize+1;
        // console.log(totalPage);
        this.setData({
          tuocaos: res.data,
          tuocao: tucaoslice,
          totalPage:totalPage
        })
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  sliceArray: function (arr, start, end) {
    var tucao = []
    for (var i = 0; i < arr.length; i++) {
      if (start <= i && i < end) {
        tucao.push(arr[i]);
      }
    }
    return tucao;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (true){
      app.flag = 0
      var accesstoken = wx.getStorageSync('accesstoken')
      wx.request({
        url: 'https://api.tianlala.com.cn/trashes', // 仅为示例，并非真实的接口地址
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': accesstoken
        },
        method: 'GET',
        data: {
        },
        success: (res) => {
          console.log(res, '成帖zh')
          res.data.forEach((item) => {
            var imgs = []
            for (var i = 0; i < item.images.length; i++) {
              imgs.push({
                imgurl: item.images[i],
                ishow: true
              })
            }
            item.images = imgs;
          })
          var tucaoslice = this.sliceArray(res.data, 0, 5);
          // console.log(tucaoslice,"jjjj");
          var totalPage = res.data.length % this.data.pageSize == 0 ? res.data.length / this.data.pageSize : (res.data.length - res.data.length % this.data.pageSize) / this.data.pageSize + 1;
          // console.log(totalPage);
          this.setData({
            tuocaos: res.data,
            tuocao: tucaoslice,
            totalPage: totalPage
          })
        },
        fail: (res) => {
          console.log(res, '失败')
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/trashes', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '下拉刷新')
        res.data.forEach((item) => {
          var imgs = []
          for (var i = 0; i < item.images.length; i++) {
            imgs.push({
              imgurl: item.images[i],
              ishow: true
            })
          }
          item.images = imgs;
        })
        var tucaoslice = this.sliceArray(res.data, 0, 5);
        // console.log(tucaoslice,"jjjj");
        var totalPage = res.data.length % this.data.pageSize == 0 ? res.data.length / this.data.pageSize : (res.data.length - res.data.length % this.data.pageSize) / this.data.pageSize + 1;
        // console.log(totalPage);
        this.setData({
          tuocaos: res.data,
          tuocao: tucaoslice,
          totalPage: totalPage
        })
        wx.stopPullDownRefresh();
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触地事件')
    this.setData({
      isLoading:true,
      page:  this.data.page + 1,
    })
    setTimeout(()=>{
      var tuocao = this.data.tuocao.concat(this.sliceArray(this.data.tuocaos, (this.data.page - 1) * 5, this.data.page * 5));
      console.log(this.data.page,this.data.totalPage);
      this.setData({
        tuocao: tuocao,
        isLoading: false,
      })
    },2000)

    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})